/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.esiag.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.StringTokenizer;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author assia
 */
public class PlanningServlet extends HttpServlet {

    private Date date;

    public void init() throws ServletException {
        date = new Date();
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");
        if (action.equals("Suivant")) {
            date = DateUtil.nextWeek(date);
            request.setAttribute("date", date);
            getServletContext().getRequestDispatcher("/planning.jsp").forward(request, response);
        } else if (action.equals("Precedent")) {
            date = DateUtil.lastWeek(date);
            request.setAttribute("date", date);
            getServletContext().getRequestDispatcher("/planning.jsp").forward(request, response);

        } else if (action.equals("Quitter")) {
            getServletContext().getRequestDispatcher("/accueil.jsp").forward(request, response);
        } else if (action.equals("Retour")) {
            date=DateUtil.mondayOfWeek(date);
            request.setAttribute("date", date);
            getServletContext().getRequestDispatcher("/planning.jsp").forward(request, response);
        } else {

            StringTokenizer tokenizer = new StringTokenizer(action, " ");
            String NumCmd = tokenizer.nextToken();
            String datejour = tokenizer.nextToken();
            String expediteur = tokenizer.nextToken();
            String numexpediteur = tokenizer.nextToken();
            String destinataire = tokenizer.nextToken();
            String numdestinataire = tokenizer.nextToken();

            request.setAttribute("NumCmd", NumCmd);
            request.setAttribute("datejour", datejour);
            request.setAttribute("expediteur", expediteur);
            request.setAttribute("numexpediteur", numexpediteur);
            request.setAttribute("destinataire", destinataire);
            request.setAttribute("numdestinataire", numdestinataire);
            /*  String numexpediteur = tokenizer.nextToken();
            String destinataire = tokenizer.nextToken();
            String numdestinataire = tokenizer.nextToken();
            String datejour = tokenizer.nextToken();
            request.setAttribute("datejour", datejour);
            request.setAttribute("expediteur", expediteur);
            request.setAttribute("numexpediteur", numexpediteur);
            request.setAttribute("destinataire", destinataire);
            request.setAttribute("numdestinataire", numdestinataire);
             */
            getServletContext().getRequestDispatcher("/response2.jsp").forward(request, response);

        }

        /*  
        String value = request.getParameter("actionBouton");
        StringTokenizer tokenizer = new StringTokenizer(value, " ");
        String expediteur = tokenizer.nextToken();
        String numexpediteur = tokenizer.nextToken();
        String destinataire = tokenizer.nextToken();
        String numdestinataire = tokenizer.nextToken();
        request.setAttribute("expediteur", expediteur);
        request.setAttribute("numexpediteur",numexpediteur);
        request.setAttribute("destinataire",destinataire);
        request.setAttribute("numdestinataire",numdestinataire);
        getServletContext().getRequestDispatcher("/response.jsp").forward(request, response);
        //date=suivant(date);
         */
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
